package com.dnjn.happiness.biz.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Communicate;
import com.dnjn.happiness.biz.domain.CommunicateVO;
import com.dnjn.happiness.biz.domain.vo.CommunicateBean;
import com.dnjn.happiness.biz.domain.vo.StatisticalAnalysisVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 沟通 Mapper 接口
 * </p>
 *
 * @author Amber
 * @since 2023-10-04
 */
public interface CommunicateMapper extends BaseMapper<Communicate> {



    void updateComSurvyId(@Param("userId") Long userId, @Param("surveyId") Long surveyId);

    Page<Communicate> queryComByUserId(@Param("dto") Communicate domain, Page page);

    Communicate getCommunicateIdById(Long communicateId);

    @Select("SELECT IFNULL(DATEDIFF(DATE_FORMAT(NOW(),'%Y-%m-%d'),DATE_FORMAT(MAX(date_end),'%Y-%m-%d')),0) communicateCount FROM biz_communicate WHERE user_id = #{userId} AND date_end IS NOT NULL")
    Integer getCommunicateCount(Long userId);

    @Select("SELECT u.nick_name AS operNickName,c.* FROM biz_communicate c LEFT JOIN sys_user u ON c.from_user_id = u.user_id WHERE c.user_id = #{userId} ORDER BY c.communicate_id DESC LIMIT 1")
    Communicate getCommunicateByUserId(Long userId);

    Page<CommunicateVO> queryCommunicateTeam(@Param("dto") Communicate domain, Page page);

    Page<StatisticalAnalysisVO> selectStatisticalAnalysisCommunicate(@Param("count") Integer count, @Param("symbol") String symbol, @Param("likeCounts") Integer likeCounts, @Param("symbol1") String symbol1, Page page);

    Communicate getResult(@Param("userId") Long userId, @Param("lastTime") String lastTime, @Param("scoreTime") String scoreTime);

    @Update("UPDATE biz_communicate SET from_user_id = #{fromUserId},oper_user_id=#{operUserId},communicate_status = '1' WHERE communicate_status = '0' AND user_id = #{userId}")
    int toAssign(Communicate communicate);

    Page<Communicate> getCommunicatePage(@Param("page") Page page, @Param("dto") Communicate communicate);

    @Delete(" DELETE FROM biz_communicate WHERE communicate_status = '0' AND from_user_id IS NULL AND oper_user_id IS NULL AND deleted = '0' ")
    void deleteByStatus();

    Page<Communicate> getPage(@Param("page") Page page, @Param("dto") Communicate communicate);


    @Select(" SELECT user_id FROM emo_user WHERE happiness_score < (SELECT config_value FROM sys_config WHERE config_key = 'biz.happinessScore.threshold') AND deleted = '0' ")
    List<Long> queryCommunicatRequired();

    @Select(" SELECT user_id FROM biz_communicate WHERE deleted = '0' AND communicate_status = '0' AND DATEDIFF(create_time,NOW()) = -1 ")
    List<Long> queryYesterdayyCommunicatRequired();

    List<Communicate> getByScoreTime(@Param("dateNow") String dateNow, @Param("yesterday") String yesterday);

    Communicate getCommunicatePageByFromUserId(@Param("fromUserId") Long fromUserId, @Param("userId") Long userId);

    List<CommunicateBean> queryCommunicateByUserId(Communicate communicate);

    List<Long> getWeekCommunicateUserIdList(Communicate communicate);

}
